# -*- coding: utf-8 -*-
"""
PBAR_TestZspecBasicHighZFinder_SingleVer2.py

Upgraded version of PBAR_TestZspecBasicHighZFinder_Single.p.  Code taken from 
PBAR_PlotZspecBasicHighZFinderHighMemoryWorkAround.py.

Created on Wed Apr 23 14:04:03 2014

@author: jkwong
"""

#PBAR_PlotZspecBasicHighZFinderHighMemoryWorkAround.py
#
#  Loads high-z markers, make diagnostic plots.
#  This script is basically the same as PBAR_PlotZspecBasicHighZFinder
# except that this is expected to be called from command line to deal with memory issues.
#
# 10/30/2013, JK, For testing the script, cy

import glob, os, gc, objgraph
import numpy as np
import PBAR_Zspec, PBAR_Cargo
import matplotlib.pyplot as plt
## image processing stuff
#from skimage.filter import canny
#from scipy import ndimage
#from skimage import morphology
#
#from skimage.filter import threshold_otsu
#from skimage.segmentation import clear_border
#from skimage.morphology import label, closing, square
#from skimage.measure import regionprops
#
##from skimage.color import label2rgb
#import PIL
#from PIL import Image
reload(PBAR_Zspec)


dataPath = r'E:\PBAR\data4\BasicScansStandardWidthPickle'
data0Path = r'E:\PBAR\data4\HighZFinder'
plotSaveDir = r'E:\PBAR\data4\HighZFinder'

num = 11

figureSize = (16, 10)

# Global parameters

newWidth = 600
newHeight = 136
xCargoInternalBounds = np.array((38, 550))
yCargoInternalBounds = np.array((13, 106))

#cargoCountRange = np.array([0, 4e7]) # only consider parts of the zspec image that have rad below this value
cargoCountRange = np.array([0, 3e7]) # only consider parts of the zspec image that have rad below this value

ZspecCountRange = np.array([0, 275])

# make different size windows
# x and y
windowList = []
windowList.append(np.array((3, 1)))
windowList.append(np.array((5, 1)))
windowList.append(np.array((7, 1)))
windowList.append(np.array((4, 2)))
windowList.append(np.array((7, 2)))

#windowList.append(np.array((4, 4)))
#windowList.append(np.array((8, 8)))

discrimThresholdList = np.array((0, 2))
fractionPixelsList = np.array((0.8, 1.0))

# get linear discriminant coefficients, polynomial fits

datasetDescription = PBAR_Zspec.ReadCargoDataDescriptionFile(r'E:\PBAR\data4\CargoSet2.txt')

for datasetIndex, datasetDescript in enumerate(datasetDescription['scanID']):
    print(datasetIndex)
#
    if datasetIndex < 66:
        continue
#
#    if datasetIndex > 64:
#        continue

#    if datasetIndex == 63:
#        continue

    
    ######################
    ## READ IN DATA
    
    # Read Zspec
    filenameZspec = '%s-FDFC-All_SW.dat' %datasetDescription['scanID'][datasetIndex]
    fullFilenameZspec = os.path.join(dataPath, filenameZspec)
    
    # Read Cargo
    filenameCargo = 'PBAR-%s.cargoimageSW.dat' %datasetDescription['dataFile'][datasetIndex]
    fullFilenameCargo = os.path.join(dataPath, filenameCargo)

    # Read in marker files
    filenameMarker = filenameCargo.replace('cargoimageSW.dat', 'cargomarkerSW')
    fullFilenameMarker = fullFilenameCargo.replace('cargoimageSW.dat', 'cargomarkerSW')    
    
    PBAR_Zspec.PlotZspecBasicHighZFinderVer2(fullFilenameCargo, fullFilenameZspec, fullFilenameMarker, plotSaveDir, newWidth, newHeight, \
        xCargoInternalBounds, yCargoInternalBounds, cargoCountRange, ZspecCountRange, windowList, \
        discrimThresholdList, fractionPixelsList, figureSize)
 
    objgraph.show_growth(limit=10)
#       import pdb;pdb.set_trace()

    # try to free up memory
    gc.collect()